//
//  UIView+MY.m
//  MiaoYin
//
//  Created by juweitu on 2020/3/3.
//  Copyright © 2020 thy. All rights reserved.
//

#import "UIView+MY.h"

@implementation UIView (MY)

#pragma mark 获取最上面的控制器
- (UIViewController*)currentViewController {
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

- (void)moveToHorizontal:(CGFloat)horizontal toVertical:(CGFloat)vertical
{
    CGRect origionRect = self.frame;
    CGRect newRect = CGRectMake(horizontal, vertical, origionRect.size.width, origionRect.size.height);
    self.frame = newRect;
}

- (void)addBouncesAnimation
{
    CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    frameAnimation.duration = 0.35;
    frameAnimation.values = @[
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)],
                              [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]
                              ];
    [self.layer addAnimation:frameAnimation forKey:nil];
}

- (CGFloat)x
{
    return CGRectGetMinX(self.frame);
}

- (CGFloat)y
{
    return CGRectGetMinY(self.frame);
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect newFrame = self.frame;
    newFrame.size = size;
    self.frame = newFrame;
}

- (CGFloat)maxX
{
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}


- (CGFloat)width
{
    return CGRectGetWidth(self.frame);
}

- (CGFloat)height
{
    return CGRectGetHeight(self.frame);
}

- (CGFloat)boundsX
{
    return CGRectGetMinX(self.bounds);
}

- (CGFloat)boundsY
{
    return CGRectGetMinY(self.bounds);
}

- (CGFloat)boundsW
{
    return CGRectGetWidth(self.bounds);
}

- (CGFloat)boundsH
{
    return CGRectGetHeight(self.bounds);
}

static char redDotViewKey;

- (UIView *)redDotView
{
    return objc_getAssociatedObject(self, &redDotViewKey);
}

- (void)addRedDotView
{
    if(!self.redDotView)
    {
        UIView *redDotView;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.12)
        {
            redDotView = [[UIView alloc] initWithFrame:CGRectMake(self.maxX+27  , 0, 6, 6)];
            redDotView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        }else
        {
            redDotView = [[UIView alloc] initWithFrame:CGRectMake(self.maxX-1  , 0, 6, 6)];
            redDotView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        }
        redDotView.backgroundColor = [UIColor colorWithUInt:0xff3b30];
        redDotView.layer.cornerRadius = CGRectGetHeight(redDotView.frame) / 2.0;
        redDotView.layer.masksToBounds = YES;
        [self addSubview:redDotView];
        [self bringSubviewToFront:redDotView];
        
        objc_setAssociatedObject(self, &redDotViewKey, redDotView, OBJC_ASSOCIATION_RETAIN);
    }
}

static char redNumberLabelKey;

- (UILabel *)redNumberLabel
{
    return objc_getAssociatedObject(self, &redNumberLabelKey);
}

- (void)addRedNumberLabel
{
    if(!self.redNumberLabel)
    {
        UILabel *redNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.maxX - 1 , 0, 12, 12)];
        redNumberLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        redNumberLabel.backgroundColor = [UIColor colorWithUInt:0xFA5946];
        redNumberLabel.layer.cornerRadius = CGRectGetHeight(redNumberLabel.frame) / 2.0;
        redNumberLabel.textColor = [UIColor whiteColor];
        redNumberLabel.layer.masksToBounds = YES;
        redNumberLabel.font = [UIFont systemFontOfSize:10];
        [self addSubview:redNumberLabel];
        [self bringSubviewToFront:redNumberLabel];
        
        objc_setAssociatedObject(self, &redNumberLabelKey, redNumberLabel, OBJC_ASSOCIATION_RETAIN);
    }
}

- (void)setRedNumber:(NSUInteger)number
{
    if(!self.redNumberLabel)
    {
        [self addRedNumberLabel];
    }
    
    NSString *text;
    
    if (number > 99)
    {
        text = @" 99+ ";
    }
    else
    {
        text = [NSString stringWithFormat:@" %ld ",(unsigned long)number];
    }
    
    self.redNumberLabel.text = text;
    [self.redNumberLabel sizeToFit];
}

@end
